@using System.ComponentModel
@using AntDesign.TableModels

CheckStrictly:
<Switch @bind-Checked="_checkStrictly" />
<br />
<RadioGroup @bind-Value="selectionType">
    <Radio Value="SelectionType.Checkbox">Checkbox</Radio>
    <Radio Value="SelectionType.Radio">Radio</Radio>
</RadioGroup>

<Table TItem="Data" DataSource="_data" TreeChildren="item=>item.Children" OnExpand="OnExpand" @bind-SelectedRows="_selectedRows" RowExpandable="@((record)=>record.Data.Name!="Not Expandable")">
    <Selection CheckStrictly="_checkStrictly" Type="@selectionType" />
    <PropertyColumn Property="c => c.Name" />
    <PropertyColumn Property="c => c.Age" Width="12%" Sortable />
    <PropertyColumn Property="c => c.Address" Width="30%" />
    <ActionColumn>
        <Button OnClick="@(() => AddChildren(context))">Add Children</Button>
    </ActionColumn>
</Table>

@string.Join(",", _selectedRows.Select(x => x.Name))

@code {
    bool _checkStrictly;
    SelectionType selectionType = SelectionType.Checkbox;
    Data[] _data;

    IEnumerable<Data> _selectedRows = [];

    protected override async Task OnInitializedAsync()
    {
        await Task.Delay(500);
        _data = data;
    }

    private async Task OnExpand(RowData row)
    {
        var data=((RowData<Data>)row).Data;
        if (data.Children==null)
        {
            await AddChildren(data);
        }
    }

    async Task AddChildren(Data row)
    {
        await Task.Delay(1000);
        var index = row.Children?.Count() ?? 0;
        row.Children = [new() {
            Key = Guid.NewGuid().ToString(),
            Name = "Jim Green No. "+index,
            Age = 42 +index ,
            Address = "London No. 1 Lake Park",
        }, ..row.Children??[]];
    }

    Data[] data =
    {
        new() {
            Key = "1",
            Name = "John Brown sr.",
            Age = 60,
            Address = "New York No. 1 Lake Park",
            Children = new Data[] {
                new() {
                    Key = "11",
                    Name = "Jim Green",
                    Age = 42,
                    Address = "London No. 1 Lake Park",
                },
                new() {
                    Key= "12",
                    Name= "John Brown jr.",
                    Age=30,
                    Address= "New York No. 3 Lake Park",
                    Children=new Data[] {
                          new() {
                              Key= "121",
                              Name= "Jimmy Brown",
                              Age= 16,
                              Address= "New York No. 3 Lake Park",
                        },
                    }
                },
                new() {
                    Key= "13",
                    Name= "Jim Green sr.",
                    Age= 72,
                    Address= "London No. 1 Lake Park",
                    Children= new Data[]{
                        new() {
                            Key= "131",
                            Name= "Jim Green",
                            Age= 42,
                            Address= "London No. 2 Lake Park",
                            Children= new Data[] {
                                new() {
                                    Key= "1311",
                                    Name= "Jim Green jr.",
                                    Age= 25,
                                    Address= "London No. 3 Lake Park",
                                },
                                new() {
                                   Key= "1312",
                                   Name= "Jimmy Green sr.",
                                   Age= 18,
                                   Address= "London No. 4 Lake Park",
                                },
                            },
                        },
                    },
                },
            },
        },
        new() {
            Key = "2",
            Name = "Not Expandable",
            Age = 32,
            Address = "Sidney No. 1 Lake Park",
        },
    };

    public class Data
    {
        [DisplayName("Key")]
        public string Key { get; set; }

        [DisplayName("Name")]
        public string Name { get; set; }

        [DisplayName("Age")]
        public int Age { get; set; }

        [DisplayName("Address")]
        public string Address { get; set; }

        public Data[] Children { get; set; }
    }
}
